package 栈和队列相互成全;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;

/**
 * @author 冷加宝
 * @version 1.0
 */
public class ConvertQueueAndStack {
    class myQueue{
        public Stack<Integer> in;
        public Stack<Integer> out;

        public myQueue() {
            in = new Stack<Integer>();
            out = new Stack<Integer>();
        }

        public boolean empty(){
            return in.isEmpty() && out.isEmpty();
        }

        // 倒数据
        // 从in栈，把数据倒入out栈,要同时满足两个条件
        // 1) out空了，才能倒数据
        // 2) 如果倒数据，in必须倒完
        public void inToOut(){
            if(out.empty()){
                while(!in.empty()){
                    out.push(in.pop());
                }
            }
        }

        public void push(int val){
            in.push(val);
            inToOut();
        }

        public int pop(){
            inToOut();
            return out.pop();
        }

        public int peek(){
            inToOut();
            return out.peek();
        }
    }

    class MyStack{
        public Queue<Integer> queue;

        public MyStack() {
            queue = new LinkedList<Integer>();
        }

        public void push(int val){
            int x = queue.size();
            queue.offer(val);
            for(int i = 0; i < x; i++){
                queue.offer(queue.poll());
            }
        }

        public int poll(){
            return queue.poll();
        }

        public int top(){
            return queue.peek();
        }

        public boolean empty(){
            return queue.isEmpty();
        }
    }
}
